﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttpHooker.h"
#include "MkEvent/MkTimer.h"
#include "MkHttpClientPool.h"
#include "MkUtil/MkUtil.h"
#include "MkUtil/MkLog.h"
#include "MkUtil/MkBase64.h"

//#define MaxNotifyTimes  3
#define HttpRequestTimeOutSec 3
//#define NotifyOffsetSec  3
CMkHttpHooker::CMkHttpHooker(CMkEventPool* pEventPool, CMkTimer* pTimer)
    : CMkHttpClientPool(pEventPool, pTimer)
    , m_GetSystemTimer("")
    , m_PayloadCount(0)
{

}

Uint32 CMkHttpHooker::StartSystemStateTimer()
{
    return GetTimer()->AddTimer([this]() {
        m_SystemState.Update();
        NotifyKeepAlive();
    }, GetSystemStateSec * 1000, m_GetSystemTimer);
}

void CMkHttpHooker::SetUserPass(const MkString& UserName, const MkString& Password)
{
    if (!UserName.empty() && !Password.empty()) {
        MkString BaseAuthor;
        MkBase64Encode(UserName + ":" + Password, BaseAuthor);
        BaseAuthor = "Basic " + BaseAuthor;
        m_MapHeader["Authorization"] = BaseAuthor;
    } else {
        m_MapHeader.clear();
    }
    m_UserName = UserName;
    m_Password = Password;
}

Uint32 CMkHttpHooker::GetSystemInfo(Json::Value &JsonData)
{
    JsonData["cpuPercent"] = m_SystemState.GetSystemCpuPercent();
    JsonData["memPercent"] = m_SystemState.GetSystemMemPercent();
    JsonData["startTime"] = m_SystemState.GetSelfStartTime();
    JsonData["payloadCount"] = m_PayloadCount;
    return NoneError;
}

Uint32 CMkHttpHooker::NotifyKeepAlive()
{
    Json::Value JsonData;
    GetSystemInfo(JsonData);
    return NotifyMsg("KeepAlive", JsonData);
}

Uint32 CMkHttpHooker::NotifyMsg(const MkString &Action, const Json::Value &JsonData, const MkString &ServerId/*=""*/)
{
    Json::Value Request;
    if (false == Action.empty()) {
        Request["action"] = Action;
        Request["serverId"] = ServerId.empty() ? m_LocalId : ServerId;
        Request["data"] = JsonData;
    } else {
        //transport notify message
        Request = JsonData;
    }
    SendJsonRequest(MkHttpMethodPost, m_HttpUrl, m_MapUriParam, m_MapHeader, Request, nullptr, HttpRequestTimeOutSec * 1000);
    return NoneError;
}

Uint32 CMkHttpHooker::AsyncTransparentMessage(const Json::Value &Reqeust, const MkHttpResponseJsonCb& cb)
{
    return AsyncMsg("", Reqeust, cb);
}

Uint32 CMkHttpHooker::AsyncSessionActiveRequest(const MkString &Session, const MkHttpResponseJsonCb& cb)
{
    Json::Value Reqeust;
    Reqeust["session"] = Session;
    return AsyncMsg("sessionAlive", Reqeust, cb);
}

Uint32 CMkHttpHooker::AsyncMsg(const MkString &Action, const Json::Value &JsonData, const MkHttpResponseJsonCb& cb)
{
    Json::Value Request;
    if (Action.empty()) {
        Request = JsonData;
    } else {
        Request["action"] = Action;
        Request["data"] = JsonData;
        Request["serverId"] = m_LocalId;
    }
    return SendJsonRequest(MkHttpMethodPost, m_HttpUrl, m_MapUriParam, m_MapHeader, Request, cb, HttpRequestTimeOutSec * 1000);
}

Uint32 CMkHttpHooker::Release()
{
    m_LocalId.clear();
    m_HttpUrl.clear();
    m_UserName.clear();
    m_Password.clear();
    m_MapHeader.clear();
    if (!m_GetSystemTimer.empty()) {
        GetTimer()->DeleteTimer(m_GetSystemTimer);
    }
    m_GetSystemTimer.clear();
    return NoneError;
}

CMkHttpHooker::~CMkHttpHooker()
{
    Release();
}
